package com.sky.task;

import com.sky.entity.Orders;
import com.sky.mapper.OrderMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.util.List;
@Slf4j
@Component
public class OrderTask {

    @Autowired
    private OrderMapper orderMapper;

    @Scheduled(cron = "0 * * * * ?")//每分钟执行一次
    public void outTime(){
        log.info("定时任务超时订单执行------");
        List<Orders> list = orderMapper.getByStatusAndTime(Orders.PENDING_PAYMENT, LocalDateTime.now().minusMinutes(15));

        if(list != null && !list.isEmpty()){

            orderMapper.updateBatch(list);
        }
    }
    @Scheduled(cron = "0 0 1 * * ?")
    public void delivert(){
        log.info("定时任务派送订单执行------");
        List<Orders> list = orderMapper.getByStatusAndTime(Orders.DELIVERY_IN_PROGRESS, LocalDateTime.now().minusHours(1));
        if(list != null && !list.isEmpty()){
            list.forEach(i->{
                i.setStatus(Orders.COMPLETED);
                i.setDeliveryTime(LocalDateTime.now().minusHours(1));
                orderMapper.update(i);
            });
        }
    }
}
